package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.IAgencyInfoAuditDao;
import com.boss.salary.entity.AgencyExt;
import com.boss.salary.entity.AgencyInfo;
import com.boss.salary.service.IAgencyInfoAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class AgencyInfoAuditServiceImpl implements IAgencyInfoAuditService {

    @Autowired
    private IAgencyInfoAuditDao agencyInfoAuditDao;

    @Override
    public PageDTO<AgencyInfo> queryAgencyInfoPage(PaginationDTO paginationDTO, Map<String, Object> queryParam, List<String> childrenId) {
        List<AgencyInfo> agencyInfos = agencyInfoAuditDao.queryAgencyInfoPage(paginationDTO, queryParam, childrenId);
        int total = agencyInfoAuditDao.queryAgencyInfo(queryParam, childrenId);
        PageDTO<AgencyInfo> PageDTO = new PageDTO<>();
        PageDTO.setRows(agencyInfos);
        PageDTO.setTotal(total);
        return PageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAgencyInfoState(AgencyExt state) {
        agencyInfoAuditDao.updateAgencyInfoState(state);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditAgencyInfo(List<AgencyInfo> agencyInfoList) {
        for (AgencyInfo agencyInfo: agencyInfoList) {
            AgencyExt agencyExt = new AgencyExt();
            agencyExt.setAgencyId(agencyInfo.getId());
            agencyExt.setVerifyState(SystemDictConstant.IS_AUDIT);
            agencyExt.setMessage(agencyInfo.getMessage());
            agencyExt.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            agencyInfoAuditDao.updateAgencyInfoState(agencyExt);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revokeAudit(List<AgencyInfo> agencyInfoList) {
        for (AgencyInfo agencyInfo: agencyInfoList) {
            AgencyExt agencyExt = new AgencyExt();
            agencyExt.setAgencyId(agencyInfo.getId());
            agencyExt.setVerifyState(SystemDictConstant.IS_SUBMIT);
            agencyExt.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            agencyInfoAuditDao.updateAgencyInfoState(agencyExt);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void returnAgencyInfo(List<AgencyInfo> agencyInfoList) {
        for (AgencyInfo agencyInfo: agencyInfoList) {
            AgencyExt agencyExt = new AgencyExt();
            agencyExt.setAgencyId(agencyInfo.getId());
            agencyExt.setVerifyState(SystemDictConstant.IS_REJECT);
            agencyExt.setMessage(agencyInfo.getMessage());
            agencyExt.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            agencyInfoAuditDao.updateAgencyInfoState(agencyExt);
        }
    }
}
